﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;

namespace WebeTerm.Common
{
    /// <summary>
    /// 系统设置
    /// </summary>
    public static class Config
    {
        public static string GetAppSeetings(string key, string def = null)
        {
            try
            {
                var v = ConfigurationManager.AppSettings[key];
                if (string.IsNullOrEmpty(v)) v = def;
                return v;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 获取默认每页数量
        /// </summary>
        public static int RecordsPaginal
        {
            get
            {
                string setting = ConfigurationManager.AppSettings["RecordsPaginal"] ?? string.Empty;
                int value;
                return int.TryParse(setting, out value) ? value : 20;
            }
        }

        /// <summary>
        /// 获取是否测试模式
        /// </summary>
        public static bool TestMode
        {
            get
            {
                string setting = ConfigurationManager.AppSettings["TestMode"] ?? string.Empty;
                return setting.ToUpper() == "TRUE";
            }
        }

        /// <summary>
        /// 获取测试账号
        /// </summary>
        public static string TestAccount
        {
            get
            {
                return ConfigurationManager.AppSettings["TestAccount"] ?? string.Empty;
            }
        }
        
        /// <summary>
        /// 获取是否使用测试流程
        /// </summary>
        public static bool TestWorkflow
        {
            get
            {
                string setting = ConfigurationManager.AppSettings["TestWorkflow"] ?? string.Empty;
                return setting.ToUpper() == "TRUE";
            }
        }

        /// <summary>
        /// 获取是否自定义错误页面
        /// </summary>
        public static bool CustomError
        {
            get
            {
                string setting = ConfigurationManager.AppSettings["CustomError"] ?? string.Empty;
                return setting.ToUpper() == "TRUE";
            }
        }

        /// <summary>
        /// 获取是否显示详细错误信息
        /// </summary>
        public static bool ErrorDetail
        {
            get
            {
                string setting = ConfigurationManager.AppSettings["ErrorDetail"] ?? string.Empty;
                return setting.ToUpper() == "TRUE";
            }
        }

        /// <summary>
        /// 获取自定义工作流Handler
        /// </summary>
        public static string CustomWorkflowOperationHandler
        {
            get
            {
                return ConfigurationManager.AppSettings["CustomWorkflowOperationHandler"] ?? string.Empty;
            }
        }

        #region 单点登录所需参数
        /// <summary>
        /// 单点登录地址
        /// </summary>
        public static string SSOLoginURL
        {
            get
            {
                return ConfigurationManager.AppSettings["SSOLoginURL"] ?? string.Empty;
            }
        }

        /// <summary>
        /// 单点登录注销地址
        /// </summary>
        public static string SSOLogoutURL
        {
            get
            {
                return ConfigurationManager.AppSettings["SSOLogoutURL"] ?? string.Empty;
            }
        }

        /// <summary>
        /// 登录成功后返回地址
        /// </summary>
        public static string HandlerURL
        {
            get
            {
                return ConfigurationManager.AppSettings["HandlerURL"] ?? string.Empty;
            }
        }
        /// <summary>
        /// 本应用注销地址
        /// </summary>
        public static string LogoutURL
        {
            get
            {
                return ConfigurationManager.AppSettings["LogoutURL"] ?? string.Empty;
            }
        }

        /// <summary>
        /// 本应用ID
        /// </summary>
        public static string ApplicationCode
        {
            get
            {
                return ConfigurationManager.AppSettings["ApplicationCode"] ?? "HTPCMS";
            }
        }
        #endregion

        #region 工作流平台参数
        /// <summary>
        /// 工作流显示地址
        /// </summary>
        public static string WorkflowDisplayURL
        {
            get
            {
                return ConfigurationManager.AppSettings["WorkflowDisplayURL"] ?? string.Empty;
            }
        }

        public static string WorkflowWebURL
        {
            get
            {
                return ConfigurationManager.AppSettings["WorkflowWebURL"] ?? string.Empty;
            }
        }
        #endregion

        /// <summary>
        /// 获取HR模块是否为视图模式
        /// </summary>
        public static bool HrIsViewMode
        {
            get
            {
                string setting = ConfigurationManager.AppSettings["HrIsViewMode"] ?? string.Empty;
                return setting.ToUpper() == "TRUE";
            }
        }

        /// <summary>
        /// 距离
        /// </summary>
        public static int RemaindDays
        {
            get
            {
                string setting = ConfigurationManager.AppSettings["RemaindDays"] ?? string.Empty;
                int value;
                return int.TryParse(setting, out value) ? value : 5;
            }
        }

        /// <summary>
        /// 报表保留时间，单位：分钟
        /// </summary>
        public static int RemaindMins
        {
            get
            {
                string setting = ConfigurationManager.AppSettings["RemaindMins"] ?? string.Empty;
                int value;
                return int.TryParse(setting, out value) ? value : 25;
            }
        }

        /// <summary>
        /// 网银转账文件生成位置
        /// </summary>
        public static string TransferZipFilePath
        {
            get
            {
                return ConfigurationManager.AppSettings["TransferZipFilePath"] ?? "/TransferFile/ZipFile";
            }
        }

        /// <summary>
        /// 网银转账结果文件生成位置
        /// </summary>
        public static string TransferResultFilePath
        {
            get
            {
                return ConfigurationManager.AppSettings["TransferResultFilePath"] ?? "/TransferFile/ResultFile";
            }
        }

        /// <summary>
        /// 报表位置目录，EndWith[/]
        /// </summary>
        public static string ReportPathFolder
        {
            get
            {
                if (null == reportPathFolder)
                {

                    var s = GetAppSeetings("ReportPathFolder", "/eAccounting/");
                    if (!s.EndsWith("/")) s += "/";
                    reportPathFolder = s;
                }
                return reportPathFolder;
            }
        }
        static string reportPathFolder = null;

        /// <summary>
        /// 报表服务器URL
        /// </summary>
        public static string ReportServerUrl
        {
            get { return GetAppSeetings("ReportServerUrl", "http://xl-dev/reportserver"); }
        }

        /// <summary>
        /// 访问报表服务的账号
        /// </summary>
        public static string ReportUsername
        {
            get { return GetAppSeetings("ReportUsername", ""); }
        }

        /// <summary>
        /// 访问报表服务的账号
        /// </summary>
        public static string HostHeader
        {
            get { return GetAppSeetings("HostHeader", ""); }
        }

        /// <summary>
        /// 访问报表服务的密码
        /// </summary>
        public static string ReportPassword
        {
            get { return GetAppSeetings("ReportPassword", ""); }
        }

        /// <summary>
        /// 访问报表服务的账号的域名
        /// </summary>
        public static string ReportDomain
        {
            get { return GetAppSeetings("ReportDomain", ""); }
        }

        /// <summary>
        /// 默认收款方公司CompanyCode
        /// </summary>
        public static string DefaultPayerCompanyCode
        {
            get { return GetAppSeetings("DefaultPayerCompanyCode", ""); }
        }

        /// <summary>
        /// 默认用户密码
        /// </summary>
        public static string DefaultUserPassword
        {
            get { return GetAppSeetings("DefaultUserPassword", "e-Accounting"); }
        }

        /// <summary>
        /// 是否同步主数据
        /// </summary>
        public static bool SyncMasterData
        {
            get
            {
                if (ConfigurationManager.AppSettings["SyncMasterData"] == null)
                    return true;
                else
                    return Utility.ParseBool(ConfigurationManager.AppSettings["SyncMasterData"]).GetValueOrDefault();

            }
        }

        /// <summary>
        /// 是否同步组织结构数据
        /// </summary>
        public static bool SyncOrganizationData
        {
            get
            {
                if (ConfigurationManager.AppSettings["SyncOrganizationData"] == null)
                    return true;
                else
                    return Utility.ParseBool(ConfigurationManager.AppSettings["SyncOrganizationData"]).GetValueOrDefault();

            }
        }

        /// <summary>
        /// 站点URL
        /// </summary>
        public static string WebSiteURL
        {
            get { return GetAppSeetings("WebSiteURL", @"http://pcms:82"); }
        }
    }
}
